//
//  PriceListViewController.m
//  tangshantongjiAPP
//
//  Created by yuntai on 2018/4/3.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "PriceListViewController.h"
#import "NSString+Time.h"
#import "CommonModel.h"
#import "ProductDetailViewController.h"
#import "ProductImageUpViewController.h"

@interface PriceListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)NSMutableArray *array;
@property (weak, nonatomic) IBOutlet UITextField *searchTF;
@property (nonatomic, copy) NSString *searchStr;
@property (nonatomic, copy) NSString *typeStr;
@property (nonatomic, copy) NSString *timeStr;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, assign) BOOL isFirst;

@end

@implementation PriceListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    self.isFirst = YES;
    self.searchStr = @"";
    self.timeStr = [NSString getNowTime];
    self.timeStr =[self.timeStr stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    self.timeStr =[self.timeStr stringByReplacingOccurrencesOfString:@"月份" withString:@""];
    switch (self.type) {
        case PriceProduceUpLoad:
            self.title = @"出厂价格上传";
            self.typeStr = @"1";
            break;
            
        case PriceProduceFix:
            self.title = @"出厂价格修改";
            self.typeStr = @"1";
            break;
            
        case PriceBuyUpLoad:
            self.title = @"购进价格上传";
            self.typeStr = @"2";
            break;
            
        case PriceBuyFix:
            self.title = @"购进价格修改";
            self.typeStr = @"2";
            break;
    }
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationHeight+45, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight-45) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 55;
    tableview.sectionHeaderHeight = 30;
    tableview.tableFooterView = [[UIView alloc]init];
    [tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];
    
    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getdata];
    }];
    [self.tableview.mj_header beginRefreshing];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getdata];
    }];
    
    
    /**刷新页面通知*/
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateViewNoti) name:@"updateViewNoti" object:nil];

}


- (void)updateViewNoti{
    
    [self.tableview.mj_header beginRefreshing];
}
-(void)dealloc{
    [UpLoadModelManager sharedInstance].popvc = nil;
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (IBAction)searchAction:(UIButton *)sender {
    [self.view endEditing:YES];

    NSString *searString = [self.searchTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (searString.length == 0 && self.isFirst == YES) {
        [HUDManager showWarningWithText:@"请输入要搜索的内容"];
        return;
    }
    self.searchStr = searString;
    self.isFirst = NO;
    self.currentPage = 1;
    [self.tableview.mj_header beginRefreshing];
}


#pragma mark tableviewdelegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *pricecellid = @"pricevideocellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pricecellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pricecellid];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        UILabel *namel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, kScreenBoundWidth-40, 33)];
        namel.tag = 101;
        namel.textColor = [UIColor blackColor];
        namel.font = [UIFont boldSystemFontOfSize:16];
        [cell.contentView addSubview:namel];
        
        UIView *yuanview = [[UIView alloc]initWithFrame:CGRectMake(20, 39, 8, 8)];
        yuanview.layer.cornerRadius = 4;
        yuanview.layer.masksToBounds = YES;
        yuanview.backgroundColor = kTextDeepDarkColor;
        yuanview.tag = 100;
        [cell.contentView addSubview:yuanview];
        
        UILabel *namel2 = [[UILabel alloc]initWithFrame:CGRectMake(30, 33, kScreenBoundWidth-100, 22)];
        namel2.tag = 102;
        namel2.textColor = kTextDeepDarkColor;
        namel2.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:namel2];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 55-0.7, kScreenBoundWidth, 0.7)];
        line.backgroundColor = KSepLineColor;
        [cell.contentView addSubview:line];
    }
    
    CommonModel *model = self.array[indexPath.section];
    
    UILabel *nl = [cell.contentView viewWithTag:101];
    nl.text = model.name?:@"";
    
    UILabel *snl = [cell.contentView viewWithTag:102];
    if ([model.status isEqualToString:@"0"]) {
        if (model.img1 ) {
           snl.text = @"已上传未审核";
        }else{
            snl.text = @"未上传未审核";
        }
    }else if ([model.status isEqualToString:@"1"]){
        snl.text = @"已通过审核";
    }else{
        snl.text = @"审核未通过";
    }
    return cell;
}
    
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString *headerid =@"tableivewheadeview";
    UITableViewHeaderFooterView *headeview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerid];
    if(!headeview){
        headeview  = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:headerid];
        headeview.contentView.backgroundColor = kBGColor;
        UILabel *namel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, kScreenBoundWidth-100, 33)];
        namel.tag = 101;
        namel.textColor = kNavigationColor;
        namel.font = [UIFont boldSystemFontOfSize:16];
        [headeview.contentView addSubview:namel];
        
    }
    CommonModel *model = self.array[section];
    UILabel *nameLabel = [headeview.contentView viewWithTag:101];
    nameLabel.text = model.add_time?:@"";
    return headeview;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.array[indexPath.section];
    
    [UpLoadModelManager sharedInstance].model = [[UploadImageParamsModel alloc] init];
    [UpLoadModelManager sharedInstance].model.name = model.name;
    [UpLoadModelManager sharedInstance].model.modelid = model.modelId;
    [UpLoadModelManager sharedInstance].model.status = model.status;
    [UpLoadModelManager sharedInstance].popvc = nil;

    if (self.type == PriceProduceFix || self.type == PriceProduceUpLoad) {
        [UpLoadModelManager sharedInstance].model.type = 1;
    }else{
        [UpLoadModelManager sharedInstance].model.type = 2;
    }
    
    if (model.img1 && model.img1.length>0) {
        /**上传过*/
        ProductDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"productdetailvc"];
        vc.model = model;
        [UpLoadModelManager sharedInstance].popvc = self;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        /**未上传*/
        ProductImageUpViewController *vc= [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ProductImageUpVC"];
        [UpLoadModelManager sharedInstance].popvc = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)getdata{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kTokenID forKey:@"token"];
    [params setValue:self.searchStr forKey:@"name"];
    if (self.type == PriceBuyUpLoad || self.type == PriceProduceUpLoad) {
        [params setValue:self.timeStr forKey:@"time"];
    }
    [params setValue:self.typeStr forKey:@"type"];
    
    [NetWork PostNetWorkWithUrl:@"api/pro/getList.ajax" with:params successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        NSArray *temparray = dic[@"data"][@"data"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        [self.array addObjectsFromArray:[CommonModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];


    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];

        
    }];

}

@end
